//*******************************************************************************
// COPYRIGHT NOTES
// ---------------
// This is a sample for BCGControlBar Library Professional Edition
// Copyright (C) 1998-2016 BCGSoft Ltd.
// All rights reserved.
//
// This source code can be used, distributed or modified
// only under terms and conditions 
// of the accompanying license agreement.
//*******************************************************************************
//
// CChartAreaView.cpp : implementation file
//

#include "stdafx.h"
#include "bcgpchartexample.h"
#include "CChartAreaView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CChartAreaView

IMPLEMENT_DYNCREATE(CChartAreaView, CBCGPChartExampleView)

CChartAreaView::CChartAreaView()
	: CBCGPChartExampleView(CChartAreaView::IDD)
{
	//{{AFX_DATA_INIT(CChartAreaView)
	m_nTransparency = 60;
	m_bShowDataMarkers = TRUE;
	m_nMarkerSize = 0;
	m_nMarkerShape = 0;
	m_bShowDataLabels = TRUE;
	m_nDataLabelAngle = 2;
	m_nOrigin = 0;
	//}}AFX_DATA_INIT
}

CChartAreaView::~CChartAreaView()
{
}

void CChartAreaView::DoDataExchange(CDataExchange* pDX)
{
	CBCGPChartExampleView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChartAreaView)
	DDX_Control(pDX, IDC_DATA_LABELS, m_wndShowDataLabels);
	DDX_Control(pDX, IDC_ORIGIN_LABEL, m_wndOriginLabel);
	DDX_Control(pDX, IDC_ORIGIN, m_wndOrigin);
	DDX_Control(pDX, IDC_TRANSPARENCY, m_wndTransparency);
	DDX_Control(pDX, IDC_CHART, m_wndChart);
	DDX_Slider(pDX, IDC_TRANSPARENCY, m_nTransparency);
	DDX_Control(pDX, IDC_DATA_LABEL_ANGLE, m_wndDataLabelAngle);
	DDX_Control(pDX, IDC_DATA_MARKER_SHAPE, m_wndMarkerShape);
	DDX_Control(pDX, IDC_DATA_MARKER_SIZE, m_wndMarkerSize);
	DDX_Check(pDX, IDC_DATA_MARKERS, m_bShowDataMarkers);
	DDX_CBIndex(pDX, IDC_DATA_MARKER_SIZE, m_nMarkerSize);
	DDX_CBIndex(pDX, IDC_DATA_MARKER_SHAPE, m_nMarkerShape);
	DDX_Check(pDX, IDC_DATA_LABELS, m_bShowDataLabels);
	DDX_CBIndex(pDX, IDC_DATA_LABEL_ANGLE, m_nDataLabelAngle);
	DDX_Slider(pDX, IDC_ORIGIN, m_nOrigin);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CChartAreaView, CBCGPChartExampleView)
	//{{AFX_MSG_MAP(CChartAreaView)
	ON_WM_HSCROLL()
	ON_BN_CLICKED(IDC_DATA_LABELS, OnUpdateChart)
	ON_CBN_SELENDOK(IDC_DATA_MARKER_SHAPE, OnUpdateChart)
	ON_CBN_SELENDOK(IDC_DATA_MARKER_SIZE, OnUpdateChart)
	ON_BN_CLICKED(IDC_DATA_MARKERS, OnUpdateChart)
	ON_CBN_SELENDOK(IDC_DATA_LABEL_ANGLE, OnUpdateChart)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChartAreaView diagnostics

#ifdef _DEBUG
void CChartAreaView::AssertValid() const
{
	CBCGPChartExampleView::AssertValid();
}

void CChartAreaView::Dump(CDumpContext& dc) const
{
	CBCGPChartExampleView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CChartAreaView message handlers

void CChartAreaView::OnInitialUpdate() 
{
	CBCGPChartExampleView::OnInitialUpdate();
	
	if (m_bIsReady)
	{
		return;
	}

	m_bIsReady = TRUE;

	m_wndTransparency.SetRange(0, 100, TRUE);

	if (m_Feature == CDemoFeature::BCGP_RangeArea)
	{
		m_wndShowDataLabels.EnableWindow(FALSE);
		m_bShowDataLabels = FALSE;

		UpdateData(FALSE);
	}

	if (m_Feature == CDemoFeature::BCGP_StepArea)
	{
		m_nDataLabelAngle = 3;
		UpdateData(FALSE);
	}

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	BCGPChartType type = 
		(m_Feature == CDemoFeature::BCGP_RangeArea) ? BCGP_CT_RANGE :
		(m_Feature == CDemoFeature::BCGP_StackedArea || m_Feature == CDemoFeature::BCGP_StackedAreaSpline) ? BCGP_CT_STACKED :
		(m_Feature == CDemoFeature::BCGP_StackedArea100) ? 
		BCGP_CT_100STACKED : BCGP_CT_SIMPLE;

	pChart->SetChartType(BCGPChartArea, type);

	m_wndOrigin.SetRange(0, 20, TRUE);

	if (m_Feature != CDemoFeature::BCGP_Area)
	{
		m_wndOrigin.EnableWindow(FALSE);
		m_wndOrigin.ShowWindow(SW_HIDE);
		m_wndOriginLabel.ShowWindow(SW_HIDE);
	}

	pChart->SetChartTitle(
		(m_Feature == CDemoFeature::BCGP_RangeArea) ?
		_T("Temperature Ranges by Month/Year:") :
		_T("Average Temperatures by Month/Year:"));

	CBCGPChartSeries* pSeries1 = pChart->CreateSeries(m_arYears[2]);
	CBCGPChartSeries* pSeries2 = m_Feature == CDemoFeature::BCGP_StepArea ? NULL : pChart->CreateSeries(m_arYears[1]);
	CBCGPChartSeries* pSeries3 = m_Feature == CDemoFeature::BCGP_StepArea ? NULL : pChart->CreateSeries(m_arYears[0]);

	pChart->SetCurveType((m_Feature == CDemoFeature::BCGP_StackedAreaSpline || m_Feature == CDemoFeature::BCGP_RangeArea) ? 
							BCGPChartFormatSeries::CCT_SPLINE : 
							m_Feature == CDemoFeature::BCGP_StepArea ? BCGPChartFormatSeries::CCT_STEP :
							BCGPChartFormatSeries::CCT_LINE);

	COleDateTime today = COleDateTime::GetCurrentTime();

	double arTemp[] = { 5., 12., 19, 22, 19, 24, 22, 24, 18, 14, 10, 7 };
	for (int nMonth = 1; nMonth <= 12; nMonth++)
	{
		COleDateTime date (today.GetYear(), nMonth, 1, 0, 0, 0);
		CString strMonth = date.Format(_T("%b"));

		if (m_Feature == CDemoFeature::BCGP_RangeArea)
		{
			pChart->AddChartDataYY1(strMonth, arTemp[nMonth - 1], Rand(2, 5), 0);
			pChart->AddChartDataYY1(strMonth, (int)(arTemp[nMonth - 1] + Rand(-5, 10)), Rand(2, 5), 1);
			pChart->AddChartDataYY1(strMonth, (int)(arTemp[nMonth - 1] + Rand(-5, 10)), Rand(2, 5), 2);
		}
		else
		{
			pSeries1->AddDataPoint(strMonth, arTemp[nMonth - 1]);

			if (m_Feature != CDemoFeature::BCGP_StepArea)
			{
				pSeries2->AddDataPoint((int)(arTemp[nMonth - 1] + Rand(-5, 5)));
				pSeries3->AddDataPoint((int)(arTemp[nMonth - 1] + Rand(-5, 5)));
			}
		}
	}

	OnUpdateChart();
}

void CChartAreaView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) 
{
	CBCGPChartExampleView::OnHScroll(nSBCode, nPos, pScrollBar);

	if (pScrollBar != NULL)
	{
		OnUpdateChart();
	}
}

void CChartAreaView::OnUpdateChart()
{
	UpdateData();

	m_wndMarkerShape.EnableWindow(m_bShowDataMarkers);
	m_wndMarkerSize.EnableWindow(m_bShowDataMarkers);

	m_wndDataLabelAngle.EnableWindow(m_bShowDataLabels);

	CBCGPChartVisualObject* pChart = m_wndChart.GetChart();
	ASSERT_VALID(pChart);

	pChart->ShowDataMarkers(m_bShowDataMarkers, max(7, 5 * (m_nMarkerSize + 1)), 
		(BCGPChartMarkerOptions::MarkerShape)m_nMarkerShape);

	double dblDataLabelAngle = (m_nDataLabelAngle - 2) * 45;

	pChart->ShowDataLabels(m_bShowDataLabels, TRUE, TRUE, dblDataLabelAngle);

	pChart->SetThemeOpacity(100 - m_nTransparency);

	if (m_Feature == CDemoFeature::BCGP_Area)
	{
		for (int i = 0; i < pChart->GetSeriesCount(); i++)
		{
			CBCGPChartAreaSeries* pSeries = DYNAMIC_DOWNCAST(CBCGPChartAreaSeries, pChart->GetSeries(i));
			ASSERT_VALID(pSeries);

			pSeries->SetAreaOrigin(m_nOrigin);
		}
	}

	pChart->Redraw();
}
